package com.qf.shiroboot.shiro;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisCache<K,V> implements Cache<K,V> {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    private final String SHIRO_CACHE = "SHIRO_CACHE:";

    @Override
    public V get(K k) throws CacheException {
        System.out.println("去Redis查询授权信息");
        V v = (V) redisTemplate.opsForValue().get(SHIRO_CACHE + k);
        return v;
    }

    @Override
    public V put(K k, V v) throws CacheException {
        System.out.println("将授权信息存储在Redis");
        redisTemplate.opsForValue().set(SHIRO_CACHE + k,v,15, TimeUnit.MINUTES);
        return v;
    }

    @Override
    public V remove(K k) throws CacheException {
        return null;
    }

    @Override
    public void clear() throws CacheException {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public Set<K> keys() {
        return null;
    }

    @Override
    public Collection<V> values() {
        return null;
    }
}
